package cn.itcast.crm.service.impl;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.Path;
import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Override
    public List<Customer> findAssociationFixedAreaCustomer(String fixedAreaId ) {
        return customerRepository.findByFixedAreaId(fixedAreaId);
    }

    @Override
    public List<Customer> findNoAssociationFixedAreaCustomer() {
        return customerRepository.findByFixedAreaIdIsNull();
    }


    @Override
    public void associationCustomerToFixedArea(String customerIdStr, String fixedAreaId) {
        if (StringUtils.isBlank(customerIdStr)){
            return;
        }
        customerRepository.clearFixedAreaId(fixedAreaId);
        if ("null".equals(customerIdStr)){
            return;
        }
        String[] strs = customerIdStr.split(",");
        for (String string :strs
             ) {
            Integer id = Integer.parseInt(string);
            customerRepository.updateFixedAreaId(id,fixedAreaId);
        }
    }


}
